// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{

    /// <summary />
    partial class OrderMapper
    {

        /// <summary />
        static OrderMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Order> _FindAll(DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(Resources.Sql.OrderSelectAll);

            return Load(commands, dataMappers);
        }

        private Order.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Order.DataTransfer data=new Order.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Order.DataTransfer FillData(Order.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("OrderID");
            if (!record.IsDBNull(ord))
                data.OrderID=record.GetInt32(ord);

            ord=record.GetOrdinal("CustomerID");
            if (!record.IsDBNull(ord))
                data.CustomerID=record.GetString(ord);

            ord=record.GetOrdinal("EmployeeID");
            if (!record.IsDBNull(ord))
                data.EmployeeID=record.GetInt32(ord);

            ord=record.GetOrdinal("ShipVia");
            if (!record.IsDBNull(ord))
                data.ShipVia=record.GetInt32(ord);

            ord=record.GetOrdinal("OrderDate");
            if (!record.IsDBNull(ord))
                data.OrderDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("RequiredDate");
            if (!record.IsDBNull(ord))
                data.RequiredDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("ShippedDate");
            if (!record.IsDBNull(ord))
                data.ShippedDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("Freight");
            if (!record.IsDBNull(ord))
                data.Freight=record.GetDecimal(ord);

            ord=record.GetOrdinal("ShipName");
            if (!record.IsDBNull(ord))
                data.ShipName=record.GetString(ord);

            ord=record.GetOrdinal("ShipAddress");
            if (!record.IsDBNull(ord))
                data.ShipAddress=record.GetString(ord);

            ord=record.GetOrdinal("ShipCity");
            if (!record.IsDBNull(ord))
                data.ShipCity=record.GetString(ord);

            ord=record.GetOrdinal("ShipRegion");
            if (!record.IsDBNull(ord))
                data.ShipRegion=record.GetString(ord);

            ord=record.GetOrdinal("ShipPostalCode");
            if (!record.IsDBNull(ord))
                data.ShipPostalCode=record.GetString(ord);

            ord=record.GetOrdinal("ShipCountry");
            if (!record.IsDBNull(ord))
                data.ShipCountry=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static DbCommand _CreateSelectCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.PrimaryKey key)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderSelect,
                    parameterBuilder.GetMarker("OrderID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateDeleteCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderDelete,
                    parameterBuilder.GetMarker("OrderID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.OrderID
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateInsertCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderInsert,
                    parameterBuilder.GetMarker("CustomerID", 1),
                    parameterBuilder.GetMarker("EmployeeID", 2),
                    parameterBuilder.GetMarker("ShipVia", 3),
                    parameterBuilder.GetMarker("OrderDate", 4),
                    parameterBuilder.GetMarker("RequiredDate", 5),
                    parameterBuilder.GetMarker("ShippedDate", 6),
                    parameterBuilder.GetMarker("Freight", 7),
                    parameterBuilder.GetMarker("ShipName", 8),
                    parameterBuilder.GetMarker("ShipAddress", 9),
                    parameterBuilder.GetMarker("ShipCity", 10),
                    parameterBuilder.GetMarker("ShipRegion", 11),
                    parameterBuilder.GetMarker("ShipPostalCode", 12),
                    parameterBuilder.GetMarker("ShipCountry", 13)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CustomerID", 1),
                DbType.String,
                5,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.CustomerID ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("EmployeeID", 2),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.EmployeeID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipVia", 3),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ShipVia
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderDate", 4),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.OrderDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("RequiredDate", 5),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.RequiredDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShippedDate", 6),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ShippedDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Freight", 7),
                DbType.Decimal,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Freight
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipName", 8),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipName ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipAddress", 9),
                DbType.String,
                60,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipAddress ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipCity", 10),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipCity ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipRegion", 11),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipRegion ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipPostalCode", 12),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipPostalCode ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipCountry", 13),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipCountry ?? (object)DBNull.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateUpdateCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderUpdate,
                    parameterBuilder.GetMarker("CustomerID", 1),
                    parameterBuilder.GetMarker("EmployeeID", 2),
                    parameterBuilder.GetMarker("ShipVia", 3),
                    parameterBuilder.GetMarker("OrderDate", 4),
                    parameterBuilder.GetMarker("RequiredDate", 5),
                    parameterBuilder.GetMarker("ShippedDate", 6),
                    parameterBuilder.GetMarker("Freight", 7),
                    parameterBuilder.GetMarker("ShipName", 8),
                    parameterBuilder.GetMarker("ShipAddress", 9),
                    parameterBuilder.GetMarker("ShipCity", 10),
                    parameterBuilder.GetMarker("ShipRegion", 11),
                    parameterBuilder.GetMarker("ShipPostalCode", 12),
                    parameterBuilder.GetMarker("ShipCountry", 13),
                    parameterBuilder.GetMarker("OrderID", 14)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CustomerID", 1),
                DbType.String,
                5,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.CustomerID ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("EmployeeID", 2),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.EmployeeID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipVia", 3),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ShipVia
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderDate", 4),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.OrderDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("RequiredDate", 5),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.RequiredDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShippedDate", 6),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ShippedDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Freight", 7),
                DbType.Decimal,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Freight
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipName", 8),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipName ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipAddress", 9),
                DbType.String,
                60,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipAddress ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipCity", 10),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipCity ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipRegion", 11),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipRegion ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipPostalCode", 12),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipPostalCode ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ShipCountry", 13),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ShipCountry ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderID", 14),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.OrderID
            );

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected Salamanca.DataAccess.Data.ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private Salamanca.DataAccess.Data.ParameterBuilder _ParameterBuilder;

    }

}
